package com.example.alpha.campusbase.Fragment;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dexafree.materialList.card.Card;
import com.dexafree.materialList.card.CardProvider;
import com.dexafree.materialList.card.OnActionClickListener;
import com.dexafree.materialList.card.action.TextViewAction;
import com.dexafree.materialList.card.action.WelcomeButtonAction;
import com.dexafree.materialList.card.provider.ListCardProvider;
import com.dexafree.materialList.listeners.OnDismissCallback;
import com.dexafree.materialList.listeners.RecyclerItemClickListener;
import com.dexafree.materialList.view.MaterialListView;
import com.example.alpha.campusbase.Interface.UseMathodOfFragInActivity;
import com.example.alpha.campusbase.R;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.RequestCreator;

import java.util.ArrayList;
import java.util.List;

import jp.wasabeef.recyclerview.animators.SlideInLeftAnimator;

/**
 * Created by Alpha on 2016/6/2.
 */
public class HomeFragment extends Fragment implements UseMathodOfFragInActivity{

    private Context mToastContext;//用于Toast打印的上下文
    private Context mContext;//用于构建Card的上下文
    private MaterialListView mListView;//卡片listview

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mToastContext =getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View mHomeView = inflater.inflate(R.layout.home_layout, container, false);
        mListView= (MaterialListView) mHomeView.findViewById(R.id.home_card_list);
        mContext=getContext();

        //绑定动画
        mListView.setItemAnimator(new SlideInLeftAnimator());//绑定左划动画
        mListView.getItemAnimator().setAddDuration(300);//添加动画持续时间
        mListView.getItemAnimator().setRemoveDuration(300);//移除动画持续时间

        //若listview为空则显示默认图片
        final ImageView empty_image= (ImageView) mHomeView.findViewById(R.id.home_empty_image);
        empty_image.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        mListView.setEmptyView(empty_image);

        // TODO: 2016/6/4 网络加载图片，需要自定义或删除
        Picasso.with(getContext())
                .load("https://www.skyverge.com/wp-content/uploads/2012/05/github-logo.png")
                .resize(100,100)
                .centerInside()
                .into(empty_image);

        //填充listview
        fillArray();

        //为列表项绑定移除操作反馈
        mListView.setOnDismissCallback(new OnDismissCallback() {
            @Override
            public void onDismiss(@NonNull Card card, int position) {
                Toast.makeText(mToastContext, "You have dismissed a "
                        + card.getTag(), Toast.LENGTH_SHORT).show();
            }
        });

        mListView.addOnItemTouchListener(new RecyclerItemClickListener.OnItemClickListener() {
            //触摸事件
            @Override
            public void onItemClick(@NonNull Card card, int position) {
                // TODO: 2016/6/4 更改为进入帖子详细界面控制
                Toast.makeText(mToastContext, "You have touch a item : "
                        + card.getTag(), Toast.LENGTH_SHORT).show();
            }

            //长按事件
            @Override
            public void onItemLongClick(@NonNull Card card, int position) {
                // TODO: 2016/6/4 需要自定义长按事件
                Toast.makeText(mToastContext, "You have long touch a item : "
                        + card.getTag(), Toast.LENGTH_SHORT).show();
            }
        });

        return mHomeView;
    }

    // TODO: 2016/6/4 最终需要按照加载数量做出控制,如每次加载多少个卡片,以及卡片在数据列表的位置
    //填充卡片列表
    public void fillArray(){
        List<Card> cards=new ArrayList<>();
        for (int i=0;i<100;i++){
            cards.add(getCard(i));
        }
        mListView.getAdapter().addAll(cards);
    }


    // TODO: 2016/6/4 在服务端构建完成后修改为实际的卡片获取方法
    //获取卡片
    private Card getCard(final int position){
        String title = "Card number " + (position + 1);
        String description = "9999999999999999999";

        //随机获取卡片示例以填充数据
        switch (position % 7) {
            case 0: {
                //左下角有一个小图标的card
                return new Card.Builder(mContext)
                        .setTag("SMALL_IMAGE_CARD")
                        .setDismissible()
                        .withProvider(new CardProvider())
                        .setLayout(R.layout.material_small_image_card)
                        .setTitle(title)
                        .setDescription(description)
                        .setDrawable(R.mipmap.ic_launcher)
                        .setDrawableConfiguration(new CardProvider.OnImageConfigListener() {
                            @Override
                            public void onImageConfigure(@NonNull final RequestCreator requestCreator) {
                                requestCreator.rotate(position * 90.0f)//旋转角度
                                        .resize(150, 150)
                                        .centerCrop();
                            }
                        })
                        .endConfig()
                        .build();
            }
            case 1: {
                //带有大图像的card
                return new Card.Builder(mContext)
                        .setTag("BIG_IMAGE_CARD")
                        .withProvider(new CardProvider())
                        .setLayout(R.layout.material_big_image_card_layout)
                        .setTitle(title)
                        .setSubtitle(description)
                        .setSubtitleGravity(Gravity.END)
                        .setDrawable(
                                "https://assets-cdn.github.com/images/modules/logos_page/GitHub-Mark.png")
                        .setDrawableConfiguration(new CardProvider.OnImageConfigListener() {
                            @Override
                            public void onImageConfigure(@NonNull final RequestCreator requestCreator) {
                                requestCreator.rotate(position * 45.0f)
                                        .resize(200, 200)
                                        .centerCrop();
                            }
                        })
                        .endConfig()
                        .build();
            }
            case 2: {
                //左侧有图,右侧有个小布局的card
                final CardProvider provider = new Card.Builder(mContext)
                        .setTag("BASIC_IMAGE_BUTTON_CARD")
                        .setDismissible()
                        .withProvider(new CardProvider<>())
                        .setLayout(R.layout.material_basic_image_buttons_card_layout)
                        .setTitle(title)
                        .setSubtitle("this is subtitle")
                        .setTitleGravity(Gravity.END)
                        .setDescription(description)
                        .setDescriptionGravity(Gravity.END)
                        .setDrawable(R.mipmap.ic_launcher)
                        .setDrawableConfiguration(new CardProvider.OnImageConfigListener() {
                            @Override
                            public void onImageConfigure(@NonNull RequestCreator requestCreator) {
                                requestCreator.fit();
                            }
                        })//添加左右两个按钮并绑定事件
                        .addAction(R.id.left_text_button, new TextViewAction(mContext)
                                .setText("left")
                                .setTextResourceColor(R.color.black_button)
                                .setListener(new OnActionClickListener() {
                                    @Override
                                    public void onActionClicked(View view, Card card) {
                                        Toast.makeText(mToastContext, "You have pressed the left button",
                                                Toast.LENGTH_SHORT).show();
                                        card.getProvider().setTitle("CHANGED ON RUNTIME");
                                    }
                                }))
                        .addAction(R.id.right_text_button, new TextViewAction(mContext)
                                .setText("right")
                                .setTextResourceColor(R.color.orange_button)
                                .setListener(new OnActionClickListener() {
                                    @Override
                                    public void onActionClicked(View view, Card card) {
                                        Toast.makeText(mToastContext, "You have pressed the right button on card "
                                                + card.getProvider().getTitle(), Toast.LENGTH_SHORT).show();
                                        card.dismiss();
                                    }
                                }));

                if (position % 2 == 0) {
                    //大概是类似分割线的东西...
                    provider.setDividerVisible(true);
                }

                return provider.endConfig().build();
            }
            case 3: {
                //没有图片的标准卡片
                final CardProvider provider = new Card.Builder(mContext)
                        .setTag("BASIC_BUTTONS_CARD")
                        .setDismissible()
                        .withProvider(new CardProvider())
                        .setLayout(R.layout.material_basic_buttons_card)
                        .setTitle(title)
                        .setDescription(description)
                        .addAction(R.id.left_text_button, new TextViewAction(mContext)
                                .setText("left")
                                .setTextResourceColor(R.color.black_button)
                                .setListener(new OnActionClickListener() {
                                    @Override
                                    public void onActionClicked(View view, Card card) {
                                        Toast.makeText(mToastContext, "You have pressed the left button",
                                                Toast.LENGTH_SHORT).show();
                                    }
                                }))
                        .addAction(R.id.right_text_button, new TextViewAction(mContext)
                                .setText("right")
                                .setTextResourceColor(R.color.accent_material_dark)
                                .setListener(new OnActionClickListener() {
                                    @Override
                                    public void onActionClicked(View view, Card card) {
                                        Toast.makeText(mToastContext, "You have pressed the right button",
                                                Toast.LENGTH_SHORT).show();
                                    }
                                }));

                if (position % 2 == 0) {
                    provider.setDividerVisible(true);
                }

                return provider.endConfig().build();
            }
            case 4: {
                //纯色背景卡片,带有一个按钮
                final CardProvider provider = new Card.Builder(mContext)
                        .setTag("WELCOME_CARD")
                        .setDismissible()
                        .withProvider(new CardProvider())
                        .setLayout(R.layout.material_welcome_card_layout)
                        .setTitle("Welcome Card")
                        .setTitleColor(Color.WHITE)
                        .setDescription("I am the description")
                        .setDescriptionColor(Color.WHITE)
                        .setSubtitle("My subtitle!")
                        .setSubtitleColor(Color.WHITE)
                        .setBackgroundColor(Color.BLUE)
                        .addAction(R.id.ok_button, new WelcomeButtonAction(mContext)
                                .setText("Okay!")
                                .setTextColor(Color.WHITE)
                                .setListener(new OnActionClickListener() {
                                    @Override
                                    public void onActionClicked(View view, Card card) {
                                        Toast.makeText(mToastContext, "Welcome!", Toast.LENGTH_SHORT).show();
                                    }
                                }));

                if (position % 2 == 0) {
                    provider.setBackgroundResourceColor(android.R.color.background_dark);
                }

                return provider.endConfig().build();
            }
            case 5: {
                //没有图片的List card
                ArrayAdapter<String> adapter = new ArrayAdapter<>(mContext, android.R.layout.simple_list_item_1);
                adapter.add("Hello");
                adapter.add("World");
                adapter.add("!");

                return new Card.Builder(mContext)
                        .setTag("LIST_CARD")
                        .setDismissible()
                        .withProvider(new ListCardProvider())
                        .setLayout(R.layout.material_list_card_layout)
                        .setTitle("List Card")
                        .setDescription("Take a list")
                        .setAdapter(adapter)
                        .endConfig()
                        .build();
            }
            default: {
                final CardProvider provider = new Card.Builder(mContext)
                        .setTag("BIG_IMAGE_BUTTONS_CARD")
                        .setDismissible()
                        .withProvider(new CardProvider())
                        .setLayout(R.layout.material_image_with_buttons_card)
                        .setTitle(title)
                        .setDescription(description)
                        .setDrawable(R.mipmap.ic_launcher)
                        .addAction(R.id.left_text_button, new TextViewAction(mContext)
                                .setText("add card")
                                .setTextResourceColor(R.color.black_button)
                                .setListener(new OnActionClickListener() {
                                    @Override
                                    public void onActionClicked(View view, Card card) {
                                        Log.d("ADDING", "CARD");

                                        mListView.getAdapter().add(generateNewCard());
                                        Toast.makeText(mContext, "Added new card", Toast.LENGTH_SHORT).show();
                                    }
                                }))
                        .addAction(R.id.right_text_button, new TextViewAction(mContext)
                                .setText("right button")
                                .setTextResourceColor(R.color.accent_material_dark)
                                .setListener(new OnActionClickListener() {
                                    @Override
                                    public void onActionClicked(View view, Card card) {
                                        Toast.makeText(mToastContext, "You have pressed the right button",
                                                Toast.LENGTH_SHORT).show();
                                    }
                                }));

                if (position % 2 == 0) {
                    provider.setDividerVisible(true);
                }

                return provider.endConfig().build();
            }
        }
    }

    // TODO: 2016/6/4 需要自定义交互卡片
    //交互时产生卡片
    private Card generateNewCard() {
        return new Card.Builder(mContext)
                .setTag("BASIC_IMAGE_BUTTONS_CARD")
                .withProvider(new CardProvider())
                .setLayout(R.layout.material_basic_image_buttons_card_layout)
                .setTitle("I'm new")
                .setDescription("I've been generated on runtime!")
                .setDrawable(R.mipmap.ic_launcher)
                .endConfig()
                .build();
    }

    //在list顶部添加卡片
    public void addMockCardAtStart() {
        mListView.getAdapter().addAtStart(new Card.Builder(mContext)
                .setTag("BASIC_IMAGE_BUTTONS_CARD")
                .setDismissible()
                .withProvider(new CardProvider())
                .setLayout(R.layout.material_basic_image_buttons_card_layout)
                .setTitle("Hi there")
                .setDescription("I've been added on top!")
                .addAction(R.id.left_text_button, new TextViewAction(mContext)
                        .setText("left")
                        .setTextResourceColor(R.color.black_button))
                .addAction(R.id.right_text_button, new TextViewAction(mContext)
                        .setText("right")
                        .setTextResourceColor(R.color.orange_button))
                .setDrawable(R.mipmap.ic_launcher)
                .endConfig()
                .build());
    }

    //为MainActivity提供调用
    @Override
    public void executeMethodInFragment(int id) {
        switch (id){
            case R.id.action_clear:
                Log.d("wo jiu kankan","bu shuo hua");
                mListView.getAdapter().clearAll();
                break;
            case R.id.action_add_at_start:
                addMockCardAtStart();
                break;
        }
    }


}
